package com.rq.base.util;

import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.widget.TextView;

import com.rq.base.BASE;
import com.rq.base.R;

public class TextViewUtil {
    /**
     * @param tv           控件
     * @param con          所有内容
     * @param light        有颜色的内容
     * @param colorPrimary 颜色ID
     */
    public static void setText(TextView tv, String con, String light, int colorPrimary,
                               float colorSizeRate) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(light) || tv == null) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", " tv = " + tv + "con = " + con + " \nlight = " + light);
            return;
        }
        SpannableString spannableString = new SpannableString(con);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(tv.getContext().getResources()
                .getColor(colorPrimary));
        int start = con.indexOf(light);
        int end = start + light.length();
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(colorSizeRate);
        spannableString.setSpan(sizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
    }

    public static void setText(TextView tv, int conRes, String light, int colorPrimary,
                               float colorSizeRate) {
        if (conRes <= 0 || TextUtils.isEmpty(light) || tv == null) {
            LOG.utilLog("DATA ERROR");
            return;
        }
        String realContent = String.format(BASE.getCxt().getResources().getString(conRes), light);
        setText(tv, realContent, light, colorPrimary, colorSizeRate);
    }

    /**
     * 文字变大
     */
    public static void setTextBigger(TextView tv, String con, String light, int colorSizeRate) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(light) || tv == null) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", " tv = " + tv + "con = " + con + " \nlight = " + light);
            return;
        }
        SpannableString spannableString = new SpannableString(con);
        int start = con.indexOf(light);
        int end = start + light.length();
        start = Math.max(0, start);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(colorSizeRate);
        spannableString.setSpan(sizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
    }

    public static CharSequence getTextBigger(String con, String light, float colorSizeRate) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(light)) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", "con = " + con + " \nlight = " + light);
            return "";
        }
        SpannableString spannableString = new SpannableString(con);
        int start = con.indexOf(light);
        int end = start + light.length();
        start = Math.max(0, start);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(colorSizeRate);
        spannableString.setSpan(sizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 文字变大
     */
    public static CharSequence getTextColor(String con, String light, int color) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(light)) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", " tv = " + con + " \nlight = " + light);
            return "";
        }
        SpannableString spannableString = new SpannableString(con);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(BASE.getCxt().getResources()
                .getColor(color));
        int start = con.indexOf(light);
        int end = start + light.length();
        spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 添加下划线（light 下）
     */
    public static CharSequence getTextLine(String con, String light) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(light)) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", " tv = " + con + " \nlight = " + light);
            return "";
        }
        SpannableString spannableString = new SpannableString(con);
        UnderlineSpan line = new UnderlineSpan();
        int start = con.indexOf(light);
        int end = start + light.length();
        spannableString.setSpan(line, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 前两个变黑 第一个尺寸变大
     */
    public static CharSequence get123(String bigAndColor, String color, String normal) {
        return getTextColorSize(bigAndColor + color + normal, bigAndColor + color, bigAndColor, R.color.text_color_black, 1.6F);
    }

    public static CharSequence getTextColorSize(String con, String change, int color,
                                                float colorSizeRate) {
        return getTextColorSize(con, change, change, color, colorSizeRate);
    }

    public static CharSequence getTextColorSize(String con, String colorChange, String sizeChange, int color,
                                                float colorSizeRate) {
        return getTextColorSize(con, colorChange, sizeChange, color, colorSizeRate, false);
    }

    public static CharSequence getTextColorSize(String con, String colorChange, String sizeChange, int color,
                                                float colorSizeRate, boolean isBold) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(colorChange) || TextUtils.isEmpty(sizeChange)) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", " tv = " + con + " \nlight = " + colorChange);
            return "";
        }
        SpannableString spannableString = new SpannableString(con);
        ForegroundColorSpan colorSpan = null;
        try {
            colorSpan = new ForegroundColorSpan(BASE.getCxt().getResources().getColor(color));
        } catch (Exception e) {
            colorSpan = new ForegroundColorSpan(color);
        }
        int colorStart = con.indexOf(colorChange);
        int sizeStart = con.indexOf(sizeChange);
        int colorEnd = colorStart + colorChange.length();
        int sizeEnd = colorStart + sizeChange.length();
        spannableString.setSpan(colorSpan, colorStart, colorEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if (isBold) {
            StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
            spannableString.setSpan(styleSpan, sizeStart, sizeEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(colorSizeRate);
        spannableString.setSpan(sizeSpan, sizeStart, sizeEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static void setTextBigger(TextView tv, int conRes, String light, float colorSizeRate) {
        if (conRes <= 0 || TextUtils.isEmpty(light) || tv == null) {
            LOG.utilLog("DATA ERROR");
            return;
        }
        String realContent = String.format(BASE.getCxt().getResources().getString(conRes), light);
        setTextBigger(tv, realContent, light, colorSizeRate);
    }

    /**
     * 文字变大
     */
    public static void setTextBigger(TextView tv, String con, String light, float colorSizeRate) {
        if (TextUtils.isEmpty(con) || TextUtils.isEmpty(light) || tv == null) {
            LOG.utilLog("DATA ERROR");
            LOG.e("TextViewUtil", " tv = " + tv + "con = " + con + " \nlight = " + light);
            return;
        }
        SpannableString spannableString = new SpannableString(con);
        int start = con.indexOf(light);
        int end = start + light.length();
        start = Math.max(0, start);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(colorSizeRate);
        spannableString.setSpan(sizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tv.setText(spannableString);
    }

    /**
     * @param con           全部内容
     * @param light         变化内容
     * @param colorSizeRate 变化部分相对基础尺寸比例
     * @param colorClick    变化部分颜色
     * @param listener      变化部分点击事件
     */
    public static void setColorAndClick(TextView tv, String con, String light, float colorSizeRate,
                                        int colorClick, final View.OnClickListener listener) {
        if (tv == null || TextUtils.isEmpty(con) || TextUtils.isEmpty(light)) {
            return;
        }
        int start = Math.max(0, con.indexOf(light));
        int end = Math.min(start + light.length(), con.length());
        SpannableString spannableString = new SpannableString(con);
        if (colorSizeRate > 0) {
            RelativeSizeSpan sizeSpan = new RelativeSizeSpan(colorSizeRate);
            spannableString.setSpan(sizeSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        if (listener != null) {
            spannableString
                    .setSpan(new ClickSpan(listener), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
        }
        if (colorClick > 0) {
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(tv.getResources()
                    .getColor(colorClick));
            spannableString.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        tv.setText(spannableString);
    }

    private static class ClickSpan extends ClickableSpan {
        private View.OnClickListener mOnClickListener;

        ClickSpan(View.OnClickListener clickListener) {
            this.mOnClickListener = clickListener;
        }

        @Override
        public void onClick(View widget) {
            mOnClickListener.onClick(widget);
        }

        @Override
        public void updateDrawState(TextPaint tp) {
            tp.setColor(tp.linkColor);
            tp.setUnderlineText(false);
        }
    }


    //    public static void setRichText(TextView tv, String con) {
    //        if (tv != null && !TextUtils.isEmpty(con)) {
    //            if (con.contains(".gif")) {//显示Gif图需关闭硬件加速
    //                tv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    //            }
    //            RichText.from(con).into(tv);
    //        }
    //    }
}
